package io.wongxd.solution.compose.common

import androidx.compose.foundation.Canvas
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ColumnScope
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.RowScope
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.statusBarsPadding
import androidx.compose.foundation.layout.width
import androidx.compose.material.IconButton
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.CornerRadius
import androidx.compose.ui.geometry.center
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.PathEffect
import androidx.compose.ui.graphics.drawscope.Stroke
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.google.accompanist.systemuicontroller.rememberSystemUiController
import io.wongxd.solution.R
import io.wongxd.solution.compose.composeTheme.AppTheme
import io.wongxd.solution.compose.composeTheme.AppThemeHolder
import io.wongxd.solution.compose.core.modifier.throttleClick
import io.wongxd.solution.compose.custom.ThemeAdapterImage


@Composable
fun H(number: Int) {
    Box(modifier = Modifier.width(number.dp))
}

@Composable
fun V(number: Int) {
    Box(modifier = Modifier.height(number.dp))
}

@Composable
fun RowScope.HS() {
    Box(modifier = Modifier.weight(1f))
}

@Composable
fun ColumnScope.VS() {
    Box(modifier = Modifier.weight(1f))
}


@Composable
fun DashV(modifier: Modifier, color: Color) {
    Canvas(modifier = modifier) {
        drawLine(
            color, size.center.copy(y = 0f), size.center.copy(y = size.height),
            pathEffect = PathEffect.dashPathEffect(floatArrayOf(10f, 10f), 0f)
        )
    }
}

@Composable
fun DashH(modifier: Modifier, color: Color) {
    Canvas(modifier = modifier) {
        drawLine(
            color, size.center.copy(x = 0f), size.center.copy(x = size.width),
            pathEffect = PathEffect.dashPathEffect(floatArrayOf(10f, 10f), 0f)
        )
    }
}

@Composable
fun DashRect(modifier: Modifier, color: Color, corner: Float) {
    val stroke = Stroke(
        width = 2f,
        pathEffect = PathEffect.dashPathEffect(floatArrayOf(10f, 10f), 0f)
    )

    Canvas(modifier = modifier) {
        drawRoundRect(color = color, style = stroke, cornerRadius = CornerRadius(corner))
    }
}

@Preview
@Composable
private fun DashPreview() {
    Column {
        DashV(
            modifier = Modifier
                .padding(horizontal = 10.dp)
                .height(76.dp)
                .width(5.dp), color = AppThemeHolder.appColor
        )


        DashH(
            modifier = Modifier
                .padding(horizontal = 10.dp)
                .width(76.dp)
                .height(5.dp), color = AppThemeHolder.appColor
        )
    }
}


@Composable
fun AvoidTop(
    bgColor: Color = Color.White,
    darkIcon: Boolean = true,
    content: @Composable ColumnScope.() -> Unit
) {
    AppTheme {


        rememberSystemUiController().setStatusBarColor(bgColor, darkIcons = darkIcon)

        Column(
            Modifier
                .fillMaxSize()
                .background(bgColor)
        ) {
            Spacer(modifier = Modifier.statusBarsPadding().fillMaxWidth())

            content()
        }

    }
}

@Preview
@Composable
fun TopBar(
    title: String = "标题",
    leftRes: Int = R.drawable.w_solution_ic_topbar_back,
    leftClick: () -> Unit = {},
    bgColor: Color = Color.Transparent,
    rightStartRes: Int = 0,
    rightStr: String = "",
    rightStrColor: Color = Color.Transparent,
    rightRes: Int = 0,
    rightClick: () -> Unit = {},
    tintWhite: Boolean = false
) {
    Row(
        modifier = Modifier
            .height(48.dp)
            .fillMaxWidth(),
        horizontalArrangement = Arrangement.Center,
        verticalAlignment = Alignment.CenterVertically
    ) {
        Box(Modifier.weight(1f)) {
            if (leftRes != 0)
                IconButton(onClick = { leftClick.invoke() }) {
                    ThemeAdapterImage(
                        painter = painterResource(id = leftRes),
                        contentDescription = "left",
                        modifier = Modifier
                            .padding(start = 20.dp)
                            .align(Alignment.CenterStart)
                            .size(22.dp)
                    )
                }
        }

        Text(text = title, color = if (tintWhite) Color.White else Color.Black, fontSize = 18.sp)

        Box(Modifier.weight(1f)) {
            Row(
                Modifier
                    .padding(end = 20.dp)
                    .align(Alignment.CenterEnd)
                    .throttleClick { rightClick.invoke() },
                verticalAlignment = Alignment.CenterVertically
            ) {
                if (rightStartRes != 0)
                    ThemeAdapterImage(
                        painter = painterResource(id = rightStartRes),
                        contentDescription = "rightStart",
                        contentScale = ContentScale.Inside,
                        modifier = Modifier
                            .size(22.dp)
                            .padding(end = 2.5.dp)
                    )
                if (rightStr.isNotBlank())
                    Text(
                        text = rightStr,
                        color = when {
                            rightStrColor != Color.Transparent -> rightStrColor
                            tintWhite -> Color.White
                            else -> Color.Black
                        },
                        fontSize = 14.sp,
                        modifier = Modifier
                    )
                if (rightRes != 0)
                    ThemeAdapterImage(
                        painter = painterResource(id = rightRes),
                        contentDescription = "right",
                        contentScale = ContentScale.Inside,
                        modifier = Modifier
                            .size(22.dp)
                            .padding(start = 2.5.dp)
                    )
            }
        }
    }
}

